package com.manage.DB;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.manage.handler.ProjectHandler;
import com.manage.handler.SqliteHelper;

public class ProjectController {
	// 数据库名称
	private static String DB_NAME = "bugmanage.db";
	// 数据库版本
	private static int DB_VERSION = 2;
	private SQLiteDatabase db;
	private SqliteProjectHelper dbSinaHelper;

	// private SqliteHelper dbHelper;

	public ProjectController(Context context) {
		dbSinaHelper = new SqliteProjectHelper(context, DB_NAME, null, DB_VERSION);
		// dbHelper = new SqliteHelper(context, DB_NAME, null, DB_VERSION);
		db = dbSinaHelper.getWritableDatabase();
	}

	public void Close() {
		db.close();
		dbSinaHelper.close();
	}

	// 创建表
	// 获取users表中的USER_ID、Access Token、Access Secret的记录
	public List<ProjectHandler> GetUserList(Boolean isSimple) {
		List<ProjectHandler> userList = new ArrayList<ProjectHandler>();
		Cursor cursor = db.query(SqliteProjectHelper.TB_NAME, null, null, null, null, null, ProjectHandler.PROJECT_ID + " DESC");
		cursor.moveToFirst();
		while (!cursor.isAfterLast() && (cursor.getString(1) != null)) {
			ProjectHandler user = new ProjectHandler();
			user.setPorjectId(cursor.getString(0));
			user.setProjectName(cursor.getString(1));
			user.setProjectDescribe(cursor.getString(2));
			user.setManagerId(cursor.getString(3));

			userList.add(user);
			cursor.moveToNext();
		}
		cursor.close();
		return userList;
	}

	public boolean isNullSinaTb() {
		Boolean flag = false;
		Cursor cursor = db.query(SqliteProjectHelper.TB_NAME, null, null, null, null, null, ProjectHandler.PROJECT_ID + " DESC");
		flag = !cursor.moveToFirst();
		cursor.close();
		return flag;
	}

	// 判断users表中的是否包含某个USER_ID的记录
	public boolean HaveUserInfo(String userId) {
		Boolean b = false;
		Cursor cursor = db.query(SqliteProjectHelper.TB_NAME, null, ProjectHandler.PROJECT_ID + "=" + userId, null, null, null,
				null);
		b = cursor.moveToFirst();
		// Log.e("HaveUserInfo", b.toString());
		cursor.close();
		return b;
	}

	public final static String PROJECT_ID = "project_id";
	public final static String PROJECT_NAME = "project_name";
	public final static String PROJECT_DESCRIBE = "project_describe";
	public final static String MANAGER_ID = "manager_id";

	// 更新users表的记录
	public int UpdateUserInfo(ProjectHandler user, Boolean isSimple) {
		ContentValues values = new ContentValues();
		// values.put(ProjectHandler.PROJECT_ID, user.getPorjectId());
		values.put(ProjectHandler.PROJECT_NAME, user.getProjectName());
		values.put(ProjectHandler.PROJECT_DESCRIBE, user.getProjectDescribe());
		values.put(ProjectHandler.MANAGER_ID, user.getManagerId());

		int id = db.update(SqliteProjectHelper.TB_NAME, values, ProjectHandler.PROJECT_ID + "=" + user.getPorjectId(), null);
		// Log.e("UpdateUserInfo", id + "");
		return id;
	}

	// 添加users表的记录
	public Long SaveUserInfo(ProjectHandler user, Boolean isSimple) {
		ContentValues values = new ContentValues();
		values.put(ProjectHandler.PROJECT_ID, user.getPorjectId());
		values.put(ProjectHandler.PROJECT_NAME, user.getProjectName());
		values.put(ProjectHandler.PROJECT_DESCRIBE, user.getProjectDescribe());
		values.put(ProjectHandler.MANAGER_ID, user.getManagerId());
		Long uid = db.insert(SqliteHelper.TB_NAME, ProjectHandler.PROJECT_ID, values);
		// Log.e("SaveUserInfo", uid + "");
		return uid;
	}

	// 删除users表的记录
	public int DelUserInfo(String userId) {
		int id = db.delete(SqliteHelper.TB_NAME, ProjectHandler.PROJECT_ID + "=" + userId, null);
		// Log.e("DelUserInfo", id + "");
		return id;
	}
}
